// Copyright (C) joyinter
// All rights reserved.
// 
// Author : deng xinguo
// Date   : 2005-5-08
#include "StdAfx.h"
#include ".\udpaddrmanager.h"
#include "UDPTransferManager.h"

extern CUDPTransferManager s_UDPTransfer;

//##ModelId=44C4B5CF01C6
CUDPAddrManager::CUDPAddrManager(void)
{
	InitAddrList();
}

//##ModelId=44C4B5CF01D4
CUDPAddrManager::~CUDPAddrManager(void)
{
}
//##ModelId=44C4B5CF01F4
void CUDPAddrManager::InitAddrList()
{
   for(int i=0;i<255;i++)
   {
	 m_PlayerAddr[i].nPlayerVirIP=300;
	 m_PlayerAddr[i].dwPlayerLocalIP=0;
	 m_PlayerAddr[i].dwPlayerMappingIP=0;
	 m_PlayerAddr[i].dwPlayerRealIP=0;
	 memset(m_PlayerAddr[i].sa_netnum,0,4);
	 memset(m_PlayerAddr[i].sa_nodenum,0,6);
	 m_PlayerAddr[i].sa_socket=0;
	 m_PlayerAddr[i].wPlayerLocalPort=0;
	 m_PlayerAddr[i].wPlayerMappingPort=0;
	 m_PlayerAddr[i].wPlayerRealPort=0;
   }
}
//##ModelId=44C4B5CF01F5
bool CUDPAddrManager::AddNewAddrNode(PLAYERADDR playaddr)
{
	short vip=playaddr.nPlayerVirIP;
	if(vip>255||vip<0)  return false;
	else
	{
		m_PlayerAddr[vip].dwPlayerLocalIP=playaddr.dwPlayerLocalIP;
		m_PlayerAddr[vip].nPlayerVirIP=playaddr.nPlayerVirIP;
		m_PlayerAddr[vip].dwPlayerMappingIP=playaddr.dwPlayerMappingIP;
		m_PlayerAddr[vip].dwPlayerRealIP=playaddr.dwPlayerRealIP;
		memcpy(m_PlayerAddr[vip].sa_netnum,playaddr.sa_netnum,4);
		memcpy(m_PlayerAddr[vip].sa_nodenum,playaddr.sa_nodenum,6);
		m_PlayerAddr[vip].sa_socket=playaddr.sa_socket;
		m_PlayerAddr[vip].wPlayerLocalPort=playaddr.wPlayerLocalPort;
		m_PlayerAddr[vip].wPlayerMappingPort=playaddr.wPlayerMappingPort;
		m_PlayerAddr[vip].wPlayerRealPort=playaddr.wPlayerRealPort;
		return true;
	}
}
//##ModelId=44C4B5CF01F7
void CUDPAddrManager::DelAddrNode(int index)
{
	if(index<0||index>254)   return;

	 m_PlayerAddr[index].nPlayerVirIP=300;
	 m_PlayerAddr[index].dwPlayerLocalIP=0;
	 m_PlayerAddr[index].dwPlayerMappingIP=0;
	 m_PlayerAddr[index].dwPlayerRealIP=0;
	 memset(m_PlayerAddr[index].sa_netnum,0,4);
	 memset(m_PlayerAddr[index].sa_nodenum,0,6);
	 m_PlayerAddr[index].sa_socket=0;
	 m_PlayerAddr[index].wPlayerLocalPort=0;
	 m_PlayerAddr[index].wPlayerMappingPort=0;
	 m_PlayerAddr[index].wPlayerRealPort=0;
}
//##ModelId=44C4B5CF01F9
bool CUDPAddrManager::IsAddrInList(short vip)
{
   if(m_PlayerAddr[vip].nPlayerVirIP==vip)
   {
	   return true;
   }
   else
   {
	   return false;
   }
}
//##ModelId=44C4B5CF0204
bool CUDPAddrManager::GetAddrByVip(short vip,PLAYERADDR &playaddr)
{
    if(!IsAddrInList(vip))   return false;
	else
	{
		playaddr.dwPlayerLocalIP=m_PlayerAddr[vip].dwPlayerLocalIP;
		playaddr.nPlayerVirIP=m_PlayerAddr[vip].nPlayerVirIP;
		playaddr.dwPlayerMappingIP=m_PlayerAddr[vip].dwPlayerMappingIP;
		playaddr.dwPlayerRealIP=m_PlayerAddr[vip].dwPlayerRealIP;
		memcpy(playaddr.sa_netnum,m_PlayerAddr[vip].sa_netnum,4);
		memcpy(playaddr.sa_nodenum,m_PlayerAddr[vip].sa_nodenum,6);
		playaddr.sa_socket=m_PlayerAddr[vip].sa_socket;
		playaddr.wPlayerLocalPort=m_PlayerAddr[vip].wPlayerLocalPort;
		playaddr.wPlayerMappingPort=m_PlayerAddr[vip].wPlayerMappingPort;
		playaddr.wPlayerRealPort=m_PlayerAddr[vip].wPlayerRealPort;
		return true;
	}
}
//##ModelId=44C4B5CF0207
int CUDPAddrManager::GetVIPByIPXAddr(char NetNum[4],char NodeNum[6])
{
	int i;
	for( i=0;i<255;i++)
	{
		if(m_PlayerAddr[i].sa_netnum[0]==NetNum[0]&&m_PlayerAddr[i].sa_netnum[1]==NetNum[1]&&m_PlayerAddr[i].sa_netnum[2]==NetNum[2]&&m_PlayerAddr[i].sa_netnum[3]==NetNum[3]&&m_PlayerAddr[i].sa_nodenum[0]==NodeNum[0]&&m_PlayerAddr[i].sa_nodenum[1]==NodeNum[1]&&m_PlayerAddr[i].sa_nodenum[2]==NodeNum[2]&&m_PlayerAddr[i].sa_nodenum[3]==NodeNum[3]&&m_PlayerAddr[i].sa_nodenum[4]==NodeNum[4]&&m_PlayerAddr[i].sa_nodenum[5]==NodeNum[5])
			break;
		
	}
	if(i>=255)  return -1;
	else  return i;
}
